package com.tgy.leetcode;

/**
 * @Author: tgy
 * @Date: 1/7/21 10:27 AM
 */
public class _547_省份数量_DFS {

    public int findCircleNum(int[][] isConnected) {

        if (isConnected == null || isConnected.length == 0 || isConnected[0].length == 0) {

            return 0;
        }

        boolean[] searchs = new boolean[isConnected.length];

        int count = 0;
        for (int i = 0; i < isConnected.length; i++) {

            if (!searchs[i]) {

                count++;
                searchs[i] = true;
                search(isConnected,searchs,i);
            }
        }

        return count;
    }

    private void search(int[][] isConnected, boolean[] searchs, int index) {


        for (int i = 0; i < isConnected[index].length; i++) {

            if (isConnected[index][i] == 1 && !searchs[i]) {

                searchs[i] = true;
                search(isConnected,searchs,i);
            }

        }

    }




}
